/**
 * 
 */
package fr.univnantes.lina.mlnlp.lexical;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @author hernandez
 *
 */
public class NGramsBuilder {

	static final String separator = " ";
	
	/**
	 * http://stackoverflow.com/questions/7591258/fast-n-gram-calculation
	 * @return
	 */
	static public List<String> ngramify (List<String> tokensList, int max) {
		List<String> ngramsList = new ArrayList<String>();

		int tokenListSize = tokensList.size();

		for (int i = 0 ; i < tokenListSize - max+1 ; i++) {
			String ngram = "";
			for (int j = i+max; j < ( tokenListSize < i+max ? tokenListSize+1  : i+ max+1) ; j++ ) {
				for (int k =i; k < j; k++ ) {
					ngram = ngram + separator + tokensList.get(k);
				}
			}
			ngram = ngram.replaceFirst(separator, "");
			//System.out.println("Debug: ngram "+ ngram);

			ngramsList.add(ngram);
		}

		return ngramsList;
	}
	
	/**
s	 * @return
	 */
	static public List<String> ngramify (String tokensString, int max) {
		
		// simple tokenizer (split by ws character)
		String tokensStringArray[] = tokensString.split("\\s+");
		//System.out.print("Debug: tokensStringArray ");
		//for (String w : tokensStringArray) {
		//	System.out.print(" "+ w);
		//}
		//System.out.println();
	    return ngramify(Arrays.asList(tokensStringArray), max);  
	}
	
	/**
	 * @return
	 */
	static public List<String> ngramify (String tokensStringArray[],  int max) {
		   
	    return ngramify(Arrays.asList(tokensStringArray), max);  
	}
	
	/**
	 * @return
	 */
	static public List<String> bigram (List<String> stringList) {  
	    return ngramify(stringList, 2);  
	}
	
	/**
	 * @return
	 */
	static public List<String> trigram (List<String> stringList) {  
	    return ngramify(stringList, 3);  
	}
	
	/**
	 * @return
	 */
	static public List<String> bigram (String string) {  
	    return ngramify(string, 2);  
	}
	
	/**
	 * @return
	 */
	static public List<String> trigram (String string) {  
	    return ngramify(string, 3);  
	}
	
}
